<?php

namespace App\Models;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Services\OrdersPayService;
use App\Models\Admin\UserModel;
use App\Utilities\Imgcompress;

class UploadPicModel extends Model {

    /**
     * 记录用户上传的图片,避免重复上传   path+md5+(big/small)+ext=图片访问地址
     * 表示唯一图片标识符,加上后缀后表示大图和小图
    比如
    表示小图 md5small.png
    表示小图  md5big.png
    */
    protected $table = 'uploadpic';
    const TypeBig='big';
    const TypeSmall='small';
    public $pic;
    public function attributeLabels() {
        return [
            'md5' => 'md5',//
            'path' => '图片路径',
            'time' => '创建时间',
            'ext' => '类型',
            'pic' => '图片',
        ];
    }


    /**
     * 添加图片路径
    */
    public static function addPic($md5,$folderName,$extension){
        $new=new self;
        $new->md5=$md5;
        $new->path=$folderName;
        $new->ext=$extension;
        $new->time=time();
        $new->save();
    }

    /**
     * 查找指定的图片
     * path+md5+(big/small)+ext=图片访问地址
    */
    public static function getFilePath($md5){
        $first= self::where([
            'md5'=>$md5
        ])->select([
            'path','ext'
        ])->first();
        if($first){
            return [
                self::TypeSmall=>$first['path'].$md5.self::TypeSmall.'.'.$first['ext'],
                self::TypeBig=>$first['path'].$md5.self::TypeBig.'.'.$first['ext']
            ];
        }else{
            return false;
        }
    }



    /**
     * 返回图片地址
    */
    public function upPic($base64)
    {
        $md5 = md5_file($base64);////原图的
        $filePath = UploadPicModel::getFilePath($md5);
        // var_dump($filePath,$md5);exit;
        if ($filePath) {

            return array(
                'code' => 0,
                'img' => Fun::pImg($filePath[UploadPicModel::TypeSmall]),
                'big' => Fun::pImg($filePath[UploadPicModel::TypeBig]),
            );
        }


        $imgData = $this->getImgData($base64);
        $folderName = '/uploads/' . date('Ymd', time()) . '/';//图片的目录
        $destinationPath = public_path() . $folderName;
        $extension = 'jpg';
        $safeName = uniqid() . '.' . $extension;
        if (!file_exists($destinationPath)) {
            mkdir($destinationPath);
        }
        $yuantuImgFilePath = $destinationPath . $safeName;

        if (!file_put_contents($yuantuImgFilePath, $imgData)) {
            OA::Json(1, '错误');
        }

        $big = $md5 . UploadPicModel::TypeBig . '.' . $extension;///大图的文件名
        $sm = $md5 . UploadPicModel::TypeSmall . '.' . $extension;///小图的文件名


        (new Imgcompress($yuantuImgFilePath, 0.8))->compressImg(
            $destinationPath . $big);

        (new Imgcompress($yuantuImgFilePath, 0.2))->compressImg(
            $destinationPath . $sm);
        unlink($yuantuImgFilePath);//删除原图
        UploadPicModel::addPic($md5, $folderName, $extension);
        return array(
            'code' => 0,
            'img' => Fun::pImg($folderName . $sm),
            'big' =>  Fun::pImg($folderName . $big),
        );
    }


    /**
     * 获取图片数据
     */
    private function getImgData($base64)
    {
        $string = 'base64,';
        $weizhi = strpos($base64, $string) + strlen($string);
        $data = substr($base64, $weizhi);
        return base64_decode($data);
    }










}
